<?php

namespace app\admin\controller\bdl\bal;

use app\admin\model\BdOrg;
use app\admin\model\Material;
use app\admin\model\Materialimage;
use app\common\controller\Backend;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Inventory extends Backend
{

    /**
     * 模型对象
     * @var \app\admin\model\Inventory
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();

        $this->model = new \app\admin\model\Inventory();
        $this->view->assign("fenableList", $this->model->getFenableList());
    }


    /**
     * 查看
     */
    public function index()
    {

        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);

        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['texturefirs','surfacefirs','materialfirs','modifier','creator','datacenter','forg','inventoryentryvalue'])
                ->where($where)
//                ->where('Inventory.fk_bdl_usableqty', '>', 0)
                ->order($sort, $order)
                ->paginate($limit);


            foreach ($list as $row)
            {
                //表面
                $surface_id     = $row['surfacefirs']['id'];
                $material_id    = $row['materialfirs']['id'];
                if(!empty($surface_id) && !empty($material_id))
                {
                    $find       = Materialimage::where('fk_bdl_bd_surface',$surface_id)->where('fk_bdl_bd_material',$material_id)->find();
                    if(!empty($find))
                    {
                        $row['surface_image'] = $find['fk_bdl_imageurl'];
                    }else{
                        $row['surface_image'] = '';
                    }
                }else{
                    $row['surface_image'] = '';
                }
            }


            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        $mater_id   = $this->request->param('ids');

        $this->assign("mater_id", $mater_id);

        return $this->view->fetch();
    }


}
